package com.hzt.agprosaleplatform.controller;

import com.hzt.agprosaleplatform.common.CustomResponse;
import com.hzt.agprosaleplatform.entity.Shop;
import com.hzt.agprosaleplatform.service.ShopService;
import com.hzt.agprosaleplatform.service.OrderService;
import com.hzt.agprosaleplatform.service.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/shopApi")
public class ShopController {

    private static final Logger logger = LoggerFactory.getLogger(ShopController.class);

    @Autowired
    private ShopService shopService;
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/getAllShops")
    public CustomResponse getAllShops() {
        try {
            List<Shop> shopList = shopService.getAllShops();
            // 打印商铺列表到控制台
            logger.info("获取到的商铺列表：{}", shopList);
            return new CustomResponse(200, "获取所有商铺成功", shopList);
        } catch (Exception e) {
            logger.error("获取商铺失败", e);
            return new CustomResponse(500, "获取商铺失败：" + e.getMessage(), null);
        }
    }

    @GetMapping("/{shopId}")
    public CustomResponse getShopById(@PathVariable("shopId") Long shopId) {
        try {
            Shop shop = shopService.selectShopByShopID(Math.toIntExact(shopId));
            if (shop != null) {
                return new CustomResponse(200, "获取商铺成功", shop);
            } else {
                return new CustomResponse(404, "未找到商铺", null);
            }
        } catch (Exception e) {
            logger.error("获取商铺失败", e);
            return new CustomResponse(500, "获取商铺失败：" + e.getMessage(), null);
        }
    }
    
    /**
     * 获取店铺统计数据
     * @param shopId
     * @return
     */
    @GetMapping("/statistics/{shopId}")
    public CustomResponse getShopStatistics(@PathVariable("shopId") Integer shopId) {
        try {
            Map<String, Object> statistics = new HashMap<>();
            
            // 获取商品总数
            int totalProducts = goodsService.countGoodsByShopId(shopId);
            statistics.put("totalProducts", totalProducts);
            
            // 获取订单统计数据
            Map<String, Object> orderStats = orderService.getShopOrderStatistics(shopId);
            statistics.putAll(orderStats);
            
            return new CustomResponse(200, "获取统计数据成功", statistics);
        } catch (Exception e) {
            logger.error("获取店铺统计数据失败", e);
            return new CustomResponse(500, "获取统计数据失败：" + e.getMessage(), null);
        }
    }
    
    /**
     * 获取店铺商品销量排行数据
     * @param shopId
     * @return
     */
    @GetMapping("/statistics/sales-ranking/{shopId}")
    public CustomResponse getShopSalesRanking(@PathVariable("shopId") Integer shopId) {
        try {
            List<Map<String, Object>> salesRanking = goodsService.getGoodsSalesRanking(shopId);
            return new CustomResponse(200, "获取商品销量排行成功", salesRanking);
        } catch (Exception e) {
            logger.error("获取商品销量排行失败", e);
            return new CustomResponse(500, "获取商品销量排行失败：" + e.getMessage(), null);
        }
    }
    
    /**
     * 获取店铺收入趋势数据
     * @param shopId
     * @return
     */
    @GetMapping("/statistics/revenue-trend/{shopId}")
    public CustomResponse getShopRevenueTrend(@PathVariable("shopId") Integer shopId) {
        try {
            List<Map<String, Object>> revenueTrend = orderService.getShopRevenueTrend(shopId);
            return new CustomResponse(200, "获取收入趋势成功", revenueTrend);
        } catch (Exception e) {
            logger.error("获取收入趋势失败", e);
            return new CustomResponse(500, "获取收入趋势失败：" + e.getMessage(), null);
        }
    }
    
    /**
     * 获取店铺订单状态统计
     * @param shopId
     * @return
     */
    @GetMapping("/statistics/order-status/{shopId}")
    public CustomResponse getShopOrderStatusStats(@PathVariable("shopId") Integer shopId) {
        try {
            List<Map<String, Object>> orderStatusStats = orderService.getShopOrderStatusStats(shopId);
            return new CustomResponse(200, "获取订单状态统计成功", orderStatusStats);
        } catch (Exception e) {
            logger.error("获取订单状态统计失败", e);
            return new CustomResponse(500, "获取订单状态统计失败：" + e.getMessage(), null);
        }
    }
}